package com.xique.door.biz.service.impl;


import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.PermissionGroup;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.domain.PermissionGroupUser;
import com.xique.door.bean.request.permissionGroup.PermissionGroupAddRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeleteRequest;
import com.xique.door.biz.service.IPermissionGroupBizService;
import com.xique.door.device.hk.bean.param.HikPermissionGroupDelParam;
import com.xique.door.device.hk.bean.response.BasicResponse;
import com.xique.door.device.hk.service.HikAccessCommandService;
import com.xique.door.service.IPermissionGroupDeviceService;
import com.xique.door.service.IPermissionGroupDomainService;
import com.xique.door.service.IPermissionGroupService;
import com.xique.door.service.IPermissionGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Deprecated
public class PermissionGroupBizServiceImpl implements IPermissionGroupBizService {

    @Autowired
    private IPermissionGroupService permissionGroupService;

    @Autowired
    private IPermissionGroupUserService permissionGroupUserService;

    @Autowired
    private IPermissionGroupDeviceService permissionGroupDeviceService;

    @Autowired
    private IPermissionGroupDomainService permissionGroupDomainService;

    @Autowired
    private HikAccessCommandService hikAccessCommandService;

    @Override
    public AjaxResult addGroup(PermissionGroupAddRequest request) {

        // TODO 需要增加判断 是否要添加到海康平台
//        HikPermissionGroupAddParam param = new HikPermissionGroupAddParam();
//        param.setGroupName(request.getGroupName());
//        PermissionGroupResponse response = hikAccessCommandService.addPermissionGroup(param);
//        if (!BasicResponse.isSuccess(response)) {
//            return AjaxResult.error("添加失败，请联系管理员");
//        }

        PermissionGroup group = new PermissionGroup();
        group.setParkId(request.getParkId());
        group.setForeignId(request.getForeignId());
        group.setForeignType(request.getForeignType());
        group.setGroupName(request.getGroupName());
        group.setGroupType(request.getGroupType());
        group.setGroupSort(request.getGroupSort());
        group.setStatus(request.getStatus());
//        group.setHkGroupId(response.getData().getGroupId());
        int count = permissionGroupService.insertPermissionGroup(group);
        if (count <= 0) {
            return AjaxResult.error("添加失败");
        }

        return AjaxResult.success("添加完成");
    }

    @Override
    public AjaxResult deleteGroup(PermissionGroupDeleteRequest request) {

        //TODO 1.删除权限组数据 2.删除权限组设备数据 3.删除权限组用户数据 4.删除用户下发到设备的信息
        Long groupId = Arrays.asList(Convert.toLongArray(request.getIds())).get(0);

        PermissionGroup group = permissionGroupService.selectPermissionGroupById(groupId);
        if (null == group) {
            return AjaxResult.error("未找到对应的权限组");
        }

        //如果权限组绑定了设备或者权限组绑定了用户则不让删除
        List<Long> groups = Collections.singletonList(groupId);
        List<PermissionGroupDevice> permissionGroupDevices = permissionGroupDeviceService.selectPermissionGroupDeviceListByGroupIds(groups);
        List<PermissionGroupUser> permissionGroupUsers = permissionGroupUserService.selectPermissionGroupUserListByGroupIds(groups);
        if (!permissionGroupDevices.isEmpty() || !permissionGroupUsers.isEmpty()){
            return AjaxResult.error("无法删除权限组，已绑定用户或者设备！");
        }

        // TODO 需要增加判断 是否要添加到海康平台
        if (!StringUtils.isEmpty(group.getHkGroupId())) {
            HikPermissionGroupDelParam param = new HikPermissionGroupDelParam();
            param.setGroupId(group.getHkGroupId());
            BasicResponse response = hikAccessCommandService.delPermissionGroup(param);
            if (!BasicResponse.isSuccess(response)) {
                return AjaxResult.error("删除失败，请联系管理员");
            }
        }

        int count = permissionGroupDomainService.deleteGroup(groupId);
        if (count != 1) {
            return AjaxResult.success("删除失败");
        }

        return AjaxResult.success("删除完成");
    }

    /**
     * 改变权限组状态
     * @author shd
     * @date 2021/7/7 10:33
     * @param groupId 权限组ID
     * @param status 状态
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult changeStatus(Long groupId, String status) {

        //如果权限组绑定了设备或者权限组绑定了用户则不让删除
        List<Long> groups = Collections.singletonList(groupId);
        List<PermissionGroupDevice> permissionGroupDevices = permissionGroupDeviceService.selectPermissionGroupDeviceListByGroupIds(groups);
        List<PermissionGroupUser> permissionGroupUsers = permissionGroupUserService.selectPermissionGroupUserListByGroupIds(groups);
        if (!permissionGroupDevices.isEmpty() || !permissionGroupUsers.isEmpty()){
            return AjaxResult.error("无法改变权限组状态，已绑定用户或者设备！");
        }

        PermissionGroup update = new PermissionGroup();
        update.setId(groupId);
        update.setStatus(status);
        int count = permissionGroupService.updatePermissionGroup(update);
        if (count <= 0) {
            return AjaxResult.error("改变权限组状态失败");
        }
        return AjaxResult.success("改变权限组状态完成");
    }
}
